require "Net/send_Msg"
require "Net/TrialHandel"
require 'View/UIBase/UIBase'

UIChaosTestRecord = UIBase.New(PanelNames.UIChaosTestRecord);
local rankCount = 0;
local change_type = 1
function UIChaosTestRecord.Init(func, _change_type)
    change_type = _change_type or 1
    UIBase.Init(UIChaosTestRecord, func);
end

--定义控件
function UIChaosTestRecord:OnCreate()
    self.myInfo = {};
    self.recordList = self:GetChild("RecordList");
    self.backBtn = self:GetChild("CloseBg");
    self.nothingController = self:GetController("change");
    self:addEvent();
    if change_type == 1 then
        self:RegisterEvent(Config.EventType.Trials_Chaos_Record_Info, self.setInfo)
    else
        -- todo
    end

    --self.CloseFom();
end

function UIChaosTestRecord:OnEnable()
    if change_type == 1 then
        TrialHandel.SendTrialsRecordInfo()
    else
        -- todo
    end
end

function UIChaosTestRecord:OnDisable()

end

function UIChaosTestRecord:OnDestroy()
    if change_type == 1 then
        self:UnRegisterEvent(Config.EventType.Trials_Chaos_Record_Info, self.setInfo, self);
    else
        -- todo
    end
end

function UIChaosTestRecord:addEvent()
    self:RegisterOnClick(self.backBtn, self.onCloseClick)
end

--function UIChaosTestRecord:changePage()
--    local self = UIChaosTestRecord;
--    local rankInfo = nil;
--    self.myInfo = logicMgr.TrialsManager.GetMyRankInfo();
--    if self.rankTypeController.selectedIndex == 0 then
--        rankInfo = logicMgr.TrialsManager.GetWorkRank();
--        if rankInfo then
--            self.listInfo = rankInfo;
--            self:setInfo();
--        else
--            TrialHandel.SendTrialsWorldRank();
--        end
--        --世界排行
--    else
--        rankInfo = logicMgr.TrialsManager.GetFriendRank();
--        --好友排行
--        if rankInfo then
--            self.listInfo = rankInfo;
--            self:setInfo();
--        else
--            TrialHandel.SendTrialsFriendRank();
--        end
--    end
--
--end

function UIChaosTestRecord:removeEvent()
    ----bt_SetBtn.onClick:Clear();
    --self.backBtn.onClick:Clear();
    --self.bg.onClick:Clear();
    --self.lastBtn.onClick:Clear();
    --self.nextBtn.onClick:Clear();
    --self.titleDecBackBtn.onClick:Clear();
end

function UIChaosTestRecord:onCloseClick()
    closeUI(PanelNames.UIChaosTestRecord);
end

function UIChaosTestRecord:setInfo()
    if change_type == 1 then
        self.recordListInfo = logicMgr.TrialsManager.GetTrialsRecordInfo();
        --初始化玩家个人信息
        self.idx = 0;
        self.recordList.itemRenderer = self.rendererRecordItem;
        self.recordList.numItems = #self.recordListInfo.records;
        if #self.recordListInfo.records <= 0 then
            self.nothingController.selectedIndex = 1;
        else
            self.nothingController.selectedIndex = 0;
        end
    else
        -- todo
    end
end

function UIChaosTestRecord.rendererRecordItem(idx, obj)
    local self = UIChaosTestRecord;
    local data = self.recordListInfo.records[idx+1];

    local name = obj:GetChild("PlayerName");
    local icon = obj:GetChild("Head"):GetChild("head"):GetChild("HeadIcon");
    local dam =  obj:GetChild("TotalDmg");
    local frame = obj:GetChild("Head"):GetChild("frame")
    name.text = data.attacker.playerName;
    icon.url = logicMgr.SkinManager.GetHeroIconBySkinId(tonumber(data.attacker.pic));
    frame.url = "ui://ICONAtlas/" .. data.attacker.picFrame
    local damage = 0;
    for k,v in pairs(data.attacker.memberStats) do
        damage = damage + tonumber(tostring(v.attack));

    end
    dam.text = getNumString(damage);

    local recordBtn = obj:GetChild("Record");
    recordBtn.onClick:Clear()
    recordBtn.onClick:Add(function()
        showUI(PanelNames.UILevelRecode, EnumConst.FightTypeConst.TRIAL, data,nil,function ()
            showUI(PanelNames.UIChaosTestRecord);
        end)

    end)
end

return UIChaosTestRecord;